package com.zh.index.btree.model;

import java.util.Collection;
import java.util.Iterator;

/**
 * Created by Administrator on 2021/7/24.
 */
public abstract class Node<T extends Comparable> {

    protected T t;

    protected BranchInnerNode<T> belongInnerNode;

    private int size = 0;


    public int size() {
        return size;
    }

    final protected void increase() {
        this.size++;
    }

    final protected void decrement() {
        this.size--;
    }


    /**
     *
     * @param t
     * @return t 相等或左边的节点
     */
    abstract public DataNode<T> findLeftDataNode(T t);

    /**
     * 分裂节点
     */
    abstract public BranchNode<T> divisionNode(int degree);

    abstract public Node<T> brorrowOrMergeNode(int degree);

    protected void setSize(int size) {
        this.size = size;
    }


    /**
     * 获取父节点, 如果不存在,返回 null
     * @return
     */
    protected BranchNode<T> getParentNode() {
        if (null == belongInnerNode)
            return null;
        return belongInnerNode.branchNode;
    }

    /**
     * 当前节点是否是根节点
     * @return
     */
    public boolean isRootNode() {
        return null == belongInnerNode;
    }

    protected void changeHead(T t) {
        if (null == belongInnerNode)
            return;
        if (null != belongInnerNode.preInnerNode)
            belongInnerNode.preInnerNode.t = t;
        else belongInnerNode.t = t;
    }

}
